When we run the program it will test each button in turn. When you press down the button on PORTB bit 0 the light is turned on. When you press the button on PORTB bit 1 the light is turned off. This gives us the effect we want.

You can have fun with this program. If you set bit 0 and bit 1 on the input port (the equivalent of holding down both buttons on our torch) you will see something interesting. The light continually flicks on and off as the program notices that first the on button is set (so turn the light on) and then the off button is set (so turn the light off).

The user of a torch implemented in this way would note that if they held both buttons down the light glows dimly. This is because the PICmicro runs so fast that we would not see the flashing, instead we would see the light at around quarter brightness (for a bonus point you could consider why the light is not at half brightness)

If the torch used mechanical buttons we would probably break the buttons doing this, so I suppose the PICmicro based solution is better! We could even change the manual to say "If both buttons are depressed the torch goes into battery save half brightness mode" and make our bug into a feature!

Load program 2.2 into the PICmicro and watch it run. Hold down both buttons and see that it does indeed give a dim light.